﻿#region File Description

//-----------------------------------------------------------------------------
// CartController.cs
//
// Author: ITU Team3 (2012)
//-----------------------------------------------------------------------------

#endregion File Description

#region Using Statements

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using rentit03_client.Models;
using rentit03_client.rentit03;

#endregion

namespace rentit03_client.Controllers
{
    public class CartController : RentitController
    {
        //
        // GET: /Cart/

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// Adds a media version to the cart
        /// </summary>
        /// <param name="versionId">The id of the version to add</param>
        /// <returns>The <seealso cref="ActionResult"/> containing the add action</returns>
        public ActionResult Add(int versionId)
        {
            var service = new ServiceClient();

            var r = service.AddToCart(versionId, Sid);
            var ro = MagicJSON.Deserialize<ReturnObj>(r);

            if (ro.Success)
            {
                r = service.ReadMediaFromVersionId(versionId, Sid);
                ro = MagicJSON.Deserialize<ReturnObj>(r);
                if (ro.Success)
                {
                    var movie = MagicJSON.Deserialize<MovieObj>(ro.Value);
                    return RedirectToAction("Index", "Movie", new { @id = movie.Id });
                }
            }

            throw new NotImplementedException(r);
        }

        /// <summary>
        /// Get attached to the current user
        /// </summary>
        /// <returns>A list of movie objects</returns>
        public List<MovieObj> GetCart()
        {
            if (String.IsNullOrWhiteSpace(Sid))
                return new List<MovieObj>();

            var service = new ServiceClient();
            var r = service.GetCart(Sid);
            var ro = MagicJSON.Deserialize<ReturnObj>(r);

            if (ro.Success)
            {
                var versionIds = MagicJSON.Deserialize<List<int>>(ro.Value);
                var cart = new List<MovieObj>();

                foreach (var versionId in versionIds)
                {
                    r = service.ReadMediaFromVersionId(versionId, Sid);
                    ro = MagicJSON.Deserialize<ReturnObj>(r);

                    if (ro.Success)
                    {
                        var movie = MagicJSON.Deserialize<MovieObj>(ro.Value);
                        var version = movie.Versions.FirstOrDefault(v => v.Id == versionId);
                        movie.Versions = new List<VersionObj> { version };
                        cart.Add(movie);
                    }
                }

                return cart;
            }

            throw new NotImplementedException(r);
        }

        /// <summary>
        /// Retrieves a list containing the purchases made of the current user
        /// </summary>
        /// <returns>The <seealso cref="ActionResult"/> containing the purchases action</returns>
        public ActionResult Purchases()
        {
            var service = new ServiceClient();

            var r = service.OverviewPurchases(Sid);
            var ro = MagicJSON.Deserialize<ReturnObj>(r);

            if (ro.Success)
            {
                //MagicJSON.Deserialize<List<PurchaseObj>>(ro.Value);
                return View();
            }

            throw new NotImplementedException();
        }

        /// <summary>
        /// Checks out the shopping cart of the current user
        /// </summary>
        /// <returns>The <seealso cref="ActionResult"/> containing the checkout action</returns>
        public ActionResult CheckOut()
        {
            var service = new ServiceClient();

            var r = service.CheckOutCart(Sid);
            var ro = MagicJSON.Deserialize<ReturnObj>(r);

            if (ro.Success)
            {
                ViewBag.Receipt = MagicJSON.Deserialize<ReceiptObj>(ro.Value);
                return View();
            }

            throw new NotImplementedException();
        }
    }
}